package com.gaohe.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.UserCardMapper;
import com.gaohe.mapper.UserClassesMapper;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.User;
import com.gaohe.pojo.UserClasses;
import com.gaohe.service.ClassesService;
import com.gaohe.service.UserClassesService;
import com.gaohe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserClassesServiceImpl extends ServiceImpl<UserClassesMapper, UserClasses>
        implements UserClassesService {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private UserClassesMapper userClassesMapper;
    @Override
    public List<UserClasses> getClassesByUserId(Long userId) {
        List<UserClasses> userClassesList = userClassesMapper.getClassesByUserId(userId);
        return userClassesList;
    }
    //总课时
    @Override
    public Long getTotalCount(Long tid) {
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.eq(Classes::getTeacherId,tid);
        long count = classesService.count(qw);
        return count;
    }
    //当月课时
    @Override
    public Long getMouthCount(Long tid) {
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.eq(Classes::getTeacherId,tid);
        //获取当前月份 1 号
        LocalDateTime minTime = LocalDateTime.of(
                LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
                LocalDateTime.MIN.getDayOfMonth(), 0, 0
        );
        //获取当前月份 最后一天
        LocalDateTime maxTime = LocalDateTime.of(
                LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
               // LocalDateTime.MAX.getDayOfMonth()  31
                LocalDateTime.now().getMonth().maxLength(), 23, 59
        );
        qw.between(Classes::getCreateTime,minTime,maxTime);
        long count = classesService.count(qw);
        return count;
    }
    // 根据课程id查询用户
    @Override
    public List<UserClasses> getUserByClassId(Long classId) {
        LambdaQueryWrapper<UserClasses> qw = new LambdaQueryWrapper<>();
        qw.eq(UserClasses::getClassId,classId);
        qw.select(UserClasses::getUserId);
        List<Long> userIds = this.listObjs(qw);
        //判断是否有人
        if (userIds.isEmpty()){
            return null;
        }
        LambdaQueryWrapper<UserClasses> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(UserClasses::getClassId,classId);
        List<UserClasses> userClassesList = this.list(qw2);
        LambdaQueryWrapper<User> uqw = new LambdaQueryWrapper<>();
        uqw.in(User::getId,userIds);
        List<User> userList = userService.list(uqw);
        for (UserClasses userClasses : userClassesList) {
            for (User user : userList) {
                if (userClasses.getUserId().equals(user.getId())){
                    userClasses.setUser(user);
                    break;
                }
            }
        }
        return userClassesList;
    }
}
